package com.ifengchao.dao.impl;

import com.ifengchao.commonUtil.DaoException;
import com.ifengchao.dao.IBatisBaseDao1;
import com.ifengchao.dao.IBatisBaseDao2;
import com.ifengchao.dao.IBatisBaseDao3;
import com.ifengchao.dao.iface.DealActAccountDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * 订单处理dao
 * @author chen
 *
 */
@Repository
public class DealActAccountDaoImpl implements DealActAccountDao {

	private static final Logger logger = Logger.getLogger(DealActAccountDaoImpl.class);
	
	@Autowired(required=true)
	private IBatisBaseDao1 iBatisBaseDao1;
	
	@Autowired(required=true)
	private IBatisBaseDao2 iBatisBaseDao2;

	@Autowired(required=true)
	private IBatisBaseDao3 iBatisBaseDao3;

	/**
	 * 根据智子云广告主id查询本地广告主id
	 */
	@Override
	public Map<String, Object> queryAdveterId(String advertiserid) throws DaoException {
		return (Map<String,Object>) iBatisBaseDao3.getSqlMapClientTemplate().queryForObject("dealUserMessageSql.queryAdveterId",advertiserid);
	}

	/**
	 * 查询广告主id查询总订单金额
	 */
	@Override
	public String queryOrderAmount(String adveterId) throws DaoException {
		logger.info("根据广告主id查询订单金额");
		Object orderAmount = iBatisBaseDao1.getSqlMapClientTemplate().queryForList("dealOrderSql.queryOrderAmount",adveterId);
		if(null != orderAmount) {
			return orderAmount.toString();
		} else {
			return "0";
		}
	}

	/**
	 * 更新账户冻结金额
	 */
	@Override
	public void upAccountAmount(Map<String,Object> map) throws DaoException {
		try{
			logger.info("调用更新广告主冻结金额存储过程");
			iBatisBaseDao2.getSqlMapClientTemplate().update("dealAcctCreditSql.upAccountAmount",map);
		} catch (Exception e) {
			logger.error("更新账户冻结金额失败>>>>>>>>>",e);
			throw new DaoException(e.getMessage());
		}
	}

	/**
	 * 记录出错账户
	 */
	@Override
	public void insertErrorAmount(Map<String,Object> map) throws DaoException {
		try{
			iBatisBaseDao2.getSqlMapClientTemplate().update("dealAcctCreditSql.insertErrorAmount",map);
		} catch (Exception e) {
			logger.error("记录出错账户失败>>>>>>>>>",e);
			throw new DaoException(e.getMessage());
		}
	}
}
